package com.jinzhi.api.controller;

import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.type.EnumErrorCode;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.utils.FileUrlUtil;
import com.jinzhi.oss.service.FileService;
import com.jinzhi.oss.service.OssService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <pre>
 *  基于jwt实现的API测试类
 * </pre>
 *
 * <small> 2018年4月27日 | Aron</small>
 *
 */
@CrossOrigin
@RestController
@RequestMapping("/api/oss/")
@AllArgsConstructor
public class AppOssController {
    @Autowired
    private OssService ossService;

    private FileService fileService;

    @Log("上传文件")
    @ResponseBody
    @PostMapping("/upload")
    @ApiImplicitParams({ @ApiImplicitParam(name = "Authorization", value = "Authorization", paramType = "header") })
    Result<String> upload(@RequestParam("file") MultipartFile file) {
        String url = "";
            //url = fileService.upload(file.getBytes(), file.getOriginalFilename());
            url = ossService.uploadFileOss(file);
        return Result.ok(url);
    }

    /**
     * 另外声明一个文件上传接口进行文件名称的展示
     * @description:
     * @author: YangQiWu
     * @date: 2023/4/19 19:34
     * @param: [file]
     * @return: com.jinzhi.common.utils.Result<java.lang.Object>
     **/
    @Log("上传文件")
    @ResponseBody
    @PostMapping("/upload1")
    @ApiImplicitParams({ @ApiImplicitParam(name = "Authorization", value = "Authorization", paramType = "header") })
    Result<Object> upload1(@RequestParam("file") MultipartFile file) {
        String url = "";
        String name = "";
        //url = fileService.upload(file.getBytes(), file.getOriginalFilename());
        url = ossService.uploadFileOss(file);
        String originalFilename = file.getOriginalFilename();
        int end = originalFilename.lastIndexOf(".");
        name = originalFilename.substring(0,end);
        Map map=new HashMap();
        map.put("url",url);
//        map.put("name", FileUrlUtil.getFileName(url));
        map.put("name",name);

        return Result.ok(map);
    }
}
